/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cfs.formbean;

import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;

import org.mybeans.form.FormBean;

import cfs.databean.Fund;

/**
 * 
 * @author danny seow
 */
public class TransitionFundForm extends FormBean {
	private HashMap<Integer, String> valueMap;
	private String inputDate;
	private Date latestDate;
	private Date userInputDateTypeDate;

	public TransitionFundForm(HttpServletRequest request, Fund[] fundList,
			Date latestDate) {
		valueMap = new HashMap<Integer, String>();
		for (int z = 0; z < fundList.length; z++) {
			int fundId = fundList[z].getFund_id();
			String valueInput = (String) request.getParameter("value" + fundId);
			int fundID = (int) fundList[z].getFund_id();
			valueMap.put(fundID, valueInput);
		}
		this.latestDate = latestDate;
		inputDate = (String) request.getParameter("dateInput");
		setUserInputDateTypeDate(inputDate);
	}

	public HashMap<Integer, String> getValueMap() {
		return valueMap;
	}

	public void setValueMap(HashMap<Integer, String> valueMap) {
		this.valueMap = valueMap;
	}

	public String getDate() {
		return inputDate;
	}

	public void setDate(String date) {
		this.inputDate = date;
	}

	public Date getUserInputDateTypeDate() {
		return userInputDateTypeDate;
	}

	public void setUserInputDateTypeDate(String userDate) {
		userInputDateTypeDate = convertStringToDate(userDate);
	}

	private Date convertStringToDate(String userDateInput) {
		try {
			DateFormat dateformat = new SimpleDateFormat("yyyy/MM/dd");
			Date newDate = new Date(dateformat.parse(userDateInput).getTime());
			if (!dateformat.format(newDate).equals(userDateInput)) {
				return null;
			}
			return newDate;
		} catch (ParseException ex) {
			return null;
		}
	}

	@Override
	public List<String> getValidationErrors() {
		ArrayList<String> errors = new ArrayList<String>();
		// Check of the Date;
		try {
			Date dateType = convertStringToDate(inputDate);
			if (dateType == null) {
				errors.add("Please input the correct Date format 'yyyy/MM/dd'");
				return errors;
			}
			if (dateType.equals(latestDate)) {
				errors.add("Transaction for " + inputDate
						+ " has already be recorded.");
			} else if (dateType.before(latestDate)) {
				errors.add("The date is before the latest date");
			}
		} catch (Exception e) {
			errors.add("Please input the correct Date format 'yyyy/MM/dd'");
			return errors;
		}
		Iterator<Entry<Integer, String>> it = valueMap.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry<Integer, String> pairs = it.next();
			int fundID = (Integer) pairs.getKey();
			String valueItem = (String) pairs.getValue();
			if (valueItem == null || valueItem.length() < 0) {
				errors.add("Value must not be null.");
			}
			double valueValue = 0.0;
			try {
				valueValue = Double.parseDouble(valueItem);
			} catch (NumberFormatException e) {
				errors.add("Please input only number value");
			}
			if (valueValue < 0.01) {
				errors.add("Value for " + fundID + " must be more than 0.01");
			}
			// Can add more validation.
		}
		return errors;
	}


}
